import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

import ChatServer 1.0

Window {
    width: 800
    height: 600
    visible: true
    title: "聊天室服务器"

    ChatServer {
        id: server
        onNewMessage: function(message) {
            logArea.append(message);
        }
        onClientConnected: function(name) {
            clientModel.append({"name": name});
        }
        onServerError: function(error) {
            errorDialog.text = error;
            errorDialog.open();
        }
    }

    Rectangle {
        anchors.fill: parent
        color: "#f0f0f0"

        Column {
            anchors.fill: parent
            padding: 20
            spacing: 15

            GroupBox {
                title: "服务器设置"
                width: parent.width - 40

                Grid {
                    columns: 2
                    spacing: 10

                    Label { text: "服务器IP:" }
                    TextField {
                        id: ipField
                        text: "192.168.10.12"
                        placeholderText: "例如: 192.168.1.100"
                    }

                    Label { text: "端口号:" }
                    TextField {
                        id: portField
                        text: "9527"
                        validator: IntValidator { bottom: 1024; top: 65535 }
                    }

                    Button {
                        text: server.isRunning ? "停止服务器" : "启动服务器"
                        onClicked: {
                            if (server.isRunning)
                            {
                                server.stopServer()
                            }
                            else
                            {
                                server.startServer(ipField.text, parseInt(portField.text))
                            }
                        }
                    }
                }
            }

            GroupBox {
                title: "客户端列表"
                width: parent.width - 40
                height: 150

                ListView {
                    id: clientList
                    anchors.fill: parent
                    model: ListModel { id: clientModel }
                    delegate: Text {
                        text: model.name
                        font.pixelSize: 14
                        padding: 5
                    }
                }
            }

            GroupBox {
                title: "服务器日志"
                width: parent.width - 40
                height: 300

                ScrollView {
                    anchors.fill: parent
                    TextArea {
                        id: logArea
                        readOnly: true
                        font.family: "Consolas"
                        font.pixelSize: 12
                    }
                }
            }

            // 在GroupBox中添加注册和登录功能
            GroupBox {
                title: "用户管理"
                width: parent.width - 40
                height: 100

                Grid {
                    columns: 2
                    spacing: 10

                    Label { text: "用户名:" }
                    TextField { id: usernameField }

                    Label { text: "密码:" }
                    TextField { id: passwordField; echoMode: TextInput.Password }

                    Button {
                        text: "注册用户"
                        onClicked: {
                            if (server.registerUser(usernameField.text, passwordField.text)) {
                                logArea.append("用户注册成功: " + usernameField.text)
                            } else {
                                logArea.append("用户注册失败")
                            }
                        }
                    }

                    Button {
                        text: "修改密码"
                        onClicked: {
                            // 添加密码修改对话框
                            passwordDialog.open()
                        }
                    }
                }
            }

        }
    }

    Dialog {
        id: errorDialog
        title: "服务器错误"
        standardButtons: Dialog.Ok
        anchors.centerIn: parent
    }

    // 添加密码修改对话框
    Dialog {
        id: passwordDialog
        title: "修改密码"
        anchors.centerIn: parent
        standardButtons: Dialog.Ok | Dialog.Cancel

        Column {
            spacing: 10
            width: parent.width

            Label { text: "旧密码:" }
            TextField { id: oldPasswordField; echoMode: TextInput.Password }

            Label { text: "新密码:" }
            TextField { id: newPasswordField; echoMode: TextInput.Password }

            Label { text: "确认新密码:" }
            TextField { id: confirmPasswordField; echoMode: TextInput.Password }
        }

        onAccepted: {
            if (newPasswordField.text !== confirmPasswordField.text) {
                logArea.append("新密码不匹配")
                return
            }

            if (server.changePassword(usernameField.text, oldPasswordField.text,
                                     newPasswordField.text)) {
                logArea.append("密码修改成功")
            } else {
                logArea.append("密码修改失败")
            }
        }
    }
}
